<?php
require_once("../../class2.php");
require_once("./includes/require_all.php");

function alphap_insert($slct, $sortby, $route) {
	$arr_pchoices= range ("A", "Z"); array_unshift($arr_pchoices, "all", "0-9");
	echo "<center class='forumheader3'><table><tr>";
	foreach ($arr_pchoices as $pchoice) {
		if ($slct==$pchoice) {echo "<td class='button textWHITE'>$pchoice</td>";}
		else echo "<td class='button textGREY' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$pchoice&sortby=$sortby&route=$route', '#tabled', '#cattable');\">$pchoice</td>";
	}
	echo "</tr></table></center>";
}

function alphat_insert($slct, $sortby, $route) {
	$rendque= "SELECT game,prefix,ganre,year,translated,translatedon,translating,rate,platform FROM gcs_main";
	switch($slct) {
		case '0-9': $rendque.=" WHERE game REGEXP '^[0-9]'"; break;
		case 'all': $rendque.=""; break;
		default: $rendque.=" WHERE game LIKE '".mysql_real_escape_string($slct)."%'";
	}
	switch($sortby) {
		case 'name': $rendque.=" ORDER BY game"; break;
		case 'ganre': $rendque.=" ORDER BY ganre"; break;
		case 'year': $rendque.=" ORDER BY year"; break;
		case 'translated': $rendque.=" ORDER BY translated"; break;
		case 'rate': $rendque.=" ORDER BY rate"; break;
		case 'platform': $rendque.=" ORDER BY platform"; break;
		default: $rendque.=" ORDER BY game";
	}
	switch($route) {
		case "asc": $rendque.=" ASC"; break;
		case "desc": $rendque.=" DESC"; break;
		default: $rendque.=" ASC"; break;
	}
	$catlisted= mysql_query($rendque);

	switch ($sortby) {
		case 'name': if ($route=='asc') {$rout[1]='desc';} else $rout[1]='asc'; break;
		case 'ganre': if ($route=='asc') {$rout[2]='desc';} else $rout[2]='asc'; break;
		case 'year': if ($route=='asc') {$rout[3]='desc';} else $rout[3]='asc'; break;
		case 'translated': if ($route=='asc') {$rout[4]='desc';} else $rout[4]='asc'; break;
		case 'rate': if ($route=='asc') {$rout[5]='desc';} else $rout[5]='asc'; break;
		case 'platform': if ($route=='asc') {$rout[6]='desc';} else $rout[6]='asc'; break;
		default: $rout[1]='asc';$rout[2]='asc';$rout[3]='asc';$rout[4]='asc';$rout[5]='asc';$rout[6]='asc';
	}
	
	echo "
		<center>
		<table WIDTH=100%>
			<tr HEIGHT=20px>
			<td class='forumheader'></td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px; min-width:70%' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=name&route=$rout[1]', '#tabled', '#cattable');\">Име на играта</td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px;' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=ganre&route=$rout[2]', '#tabled', '#cattable');\"><center>Жанр</center></td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px;' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=platform&route=$rout[6]', '#tabled', '#cattable');\"><center>Платформа</center></td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px;' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=year&route=$rout[3]', '#tabled', '#cattable');\"><center>Година</center></td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px;' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=translated&route=$rout[4]', '#tabled', '#cattable');\"><center><img src='gts/patch.gif' alt='Превод' title='Български превод'/></center></td>
			<td class='forumheader' style='cursor:pointer; padding-top: 5px;' onclick=\"insertFromURL('gts-catalog-exec.php?slct=$slct&sortby=rate&route=$rout[5]', '#tabled', '#cattable');\"><center><img src='gts/rate.png' alt='Рейтинг' title='Рейтинг'/></center></td>
			</tr>
	";
	
	while ($catgame = mysql_fetch_array($catlisted)){
		echo "
			<tr class='forumheader3'>
			<td class='forumheader'>✒</td>
			<td><span style='margin-left:5px;'></span><a href='gts-game.php?catgame=$catgame[prefix]'>$catgame[game]</a></td>
			<td><center>$catgame[ganre]</center></td>
			<td><center>";
		$x = explode("/", $catgame['platform']);
		$y = explode("/", $catgame['translatedon']);
		$z = explode("/", $catgame['translating']);
		if($catgame['translated']=='0'){
			foreach($x as $k => $v){
				if($k == count($x) - 1){
					echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>";
				}
				else{
					echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>/";
				}
			}
		}
		elseif($catgame['translated']=='1'){
			foreach($x as $k => $v){
				if(in_array($v, $z)){
					if($k == count($x) - 1){
						echo "<span style='color:yellow;cursor:help;' title='За тази платформа се прави превод в момента'>$v</span>";
					}
					else{
						echo "<span style='color:yellow;cursor:help;' title='За тази платформа се прави превод в момента'>$v</span>/";
					}
				}
				elseif(in_array($v, $y)){
					if($k == count($x) - 1){
						echo "<span style='color:green;cursor:help;' title='За тази платформа има превод'>$v</span>";
					}
					else{
						echo "<span style='color:green;cursor:help;' title='За тази платформа има превод'>$v</span>/";
					}
				}
				else{
					if($k == count($x) - 1){
						echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>";
					}
					else{
						echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>/";
					}
				}
			}
		}
		elseif($catgame['translated']=='2'){
			foreach($x as $k => $v){
				if(in_array($v, $y)){
					if($k == count($x) - 1){
						echo "<span style='color:green;cursor:help;' title='За тази платформа има превод'>$v</span>";
					}
					else{
						echo "<span style='color:green;cursor:help;' title='За тази платформа има превод'>$v</span>/";
					}
				}
				elseif(in_array($v, $z)){
					if($k == count($x) - 1){
						echo "<span style='color:yellow;cursor:help;' title='За тази платформа се прави превод в момента'>$v</span>";
					}
					else{
						echo "<span style='color:yellow;cursor:help;' title='За тази платформа се прави превод в момента'>$v</span>/";
					}
				}
				else{
					if($k == count($x) - 1){
						echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>";
					}
					else{
						echo "<span style='color:#999;cursor:help;' title='За тази платформа все още няма превод'>$v</span>/";
					}
				}
			}
		}
		echo "
			</center></td>
			<td><center>$catgame[year]</center></td>
			<td><center>";
		switch ($catgame['translated']) {
			case '0': echo "<img src='gts/no.gif' alt='Не' title='Не е започнат\nпревод'/>"; break;
			case '1': echo "<img src='gts/progress.gif' alt='Прави се' title='В процес на превод...'/>"; break;
			case '2': echo "<img src='gts/yes.gif' alt='Да' title='Преведена'/>"; break;
		}
		echo "
			</center></td>
			<td><center>$catgame[rate]</center></td>
			</tr>";
	}
	
	echo "
		</table>
		</center>
	";
	
	if ( mysql_num_rows($catlisted) == 0 ) echo "<center class='forumheader3'>Не са намерени игри, отговарящи на този критерий.</center>";
}

echo "<div id='tabled'>";
alphap_insert($_GET['slct'], $_GET['sortby'], $_GET['route']);
alphat_insert($_GET['slct'], $_GET['sortby'], $_GET['route']);
echo "</div>";
?>